package com.estronger.shopmanage.adapter.tasking;

import android.app.Activity;
import android.app.Application;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.B11Designer;
import com.estronger.shopmanage.bean.Pepole;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.utils.ConvertUtils;
import com.estronger.shopmanage.utils.Toast;
import com.estronger.shopmanage.view.RoundImageView;
import com.estronger.shopmanage.view.SelectPeopleActivity.OnSelectedListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2017-05-16.
 */

public class SelectPepoleListAdpter extends BaseAdapter {

    private ArrayList<B11Designer.Designer> mList;
    private Activity mContext;
    private OnSelectedListener selectedListener;
    private boolean ifMultiple;//是否多选

    public SelectPepoleListAdpter(Activity context, ArrayList<B11Designer.Designer> lt, boolean ifMult, OnSelectedListener listener) {
        mContext = context;
        ifMultiple = ifMult;
        initMList(lt);
        selectedListener = listener;
    }
    private void initMList(ArrayList<B11Designer.Designer> lt) {
        if (!TextUtils.isEmpty(MyApplication.selectID)) {
            if (ifMultiple && MyApplication.selectID.contains(",")) {
                List<String> codes = new ArrayList<String>();
                String[] s = MyApplication.selectID.split(",");
                Collections.addAll(codes, s);
                for (B11Designer.Designer designer : lt) {
                    for (int i=codes.size()-1; i>=0; i--) {
                       if (codes.get(i).equals(designer.code)) {
                           designer.ifChecked = 1;
                           codes.remove(i);
                           break;
                       }
                    }
                }

            }else {
                for (B11Designer.Designer designer : lt) {
                    if (MyApplication.selectID.equals(designer.code)) {
                        designer.ifChecked = 1;
                    }
                }
            }
        }
        mList = lt;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (mList != null)
            return mList.size();
        else
            return 0;
    }

    @Override
    public B11Designer.Designer getItem(int position) {
        // TODO Auto-generated method stub
        if (mList == null || mList.size() <= 0)
            return null;
        else
            return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ListItemView ltv;
        final B11Designer.Designer designer = mList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.adpter_select_pepole, null);
            ltv = new ListItemView(convertView);
            convertView.setTag(ltv);
        } else {
            ltv = (ListItemView) convertView.getTag();
        }
        if(mList.size()-1 == position){
            ltv.ivfGed.setVisibility(View.GONE);
            ltv.rlfSelepadt.setBackgroundResource(R.drawable.item_shape_bottom);
        }
        try {
            ltv.tvfName.setText(ConvertUtils.formatNull(designer.desc));
            //取得的路径用Glide来加载
            String picpath = designer.headpic;
            if (picpath != null && !picpath.equals("") && !picpath.equals("null")) {
                MyApplication.imageLoader.displayImage(NetManager.getDownloadURL() + "?filepath=" + picpath, ltv.imgfHeadPic, MyApplication.options);
            } else {
                ltv.imgfHeadPic.setImageResource(R.drawable.head_portrait);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(ifMultiple) {
            ltv.cbSelect.setVisibility(View.VISIBLE);
            ltv.cbSelect.setOnCheckedChangeListener(null);
            ltv.cbSelect.setTag(mList.get(position));
            if (designer.ifChecked == 1) {
                ltv.cbSelect.setChecked(true);
            } else {
                ltv.cbSelect.setChecked(false);
            }
            ltv.cbSelect.setOnCheckedChangeListener(checkedChangeListener);
        }else {
            ltv.cbSelect.setVisibility(View.GONE);
        }
        ltv.rlfSelepadt.setTag(R.id.item_id, position);
        ltv.rlfSelepadt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = (int) v.getTag(R.id.item_id);
                B11Designer.Designer selItem = mList.get(pos);
                if(selItem.ifChecked == 1) {
                    selItem.ifChecked = 0;
                }else {
                    selItem.ifChecked = 1;
                }
                mList.set(pos, selItem);
                notifyDataSetChanged();
                selectedListener.onSelect(selItem.code, selItem.desc);
            }
        });

        return convertView;
    }

    /*checkbox状态改变监听*/
    CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Pepole.Datas selItem = (Pepole.Datas) buttonView.getTag();
            selItem.ifChecked = isChecked?1:0;
        }
    };

    /**
     * 获取选中值
     * @return
     */
    public boolean getChecked() {
        try{
            String code = "", desc= "";
            if(mList.size() > 0) {
                for (B11Designer.Designer data : mList) {
                    if (data.ifChecked == 1) {
                        code = code + data.code + ',';
                        desc = desc + data.desc + ',';
                    }
                }
            }
            if(code.equals("")) {
                Toast.show(mContext, "请至少选择一位人员!", Toast.LENGTH_SHORT);
                return false;
            }else {
                code = code.substring(0, code.length()-1);
                desc = desc.substring(0, desc.length()-1);
            }
            MyApplication.selectID = code;
            MyApplication.selectPeople = desc;
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public class ListItemView {
         TextView tvfName;
         RoundImageView imgfHeadPic;
         RelativeLayout rlfSelepadt;
         ImageView ivfGed;
         CheckBox cbSelect;

        public ListItemView(View view){
            this.tvfName = (TextView) view.findViewById(R.id.tvname);
            this.rlfSelepadt = (RelativeLayout) view.findViewById(R.id.selpadt);
            this.imgfHeadPic = (RoundImageView) view.findViewById(R.id.headpic);
            this.ivfGed = (ImageView) view.findViewById(R.id.ivfGed);
            this.cbSelect = (CheckBox) view.findViewById(R.id.cbSelect);
        }
    }

}
